package com.example.demo.util;

import okhttp3.*;
import org.json.JSONObject;

import java.io.IOException;

public class DouyinDataFetcher {
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private static final OkHttpClient client = new OkHttpClient();

    public static void main(String[] args) {
        String videoId = "7486869421017828658"; // 替换为实际视频ID
        
        try {
            JSONObject data = getVideoData(videoId);
            System.out.println("点赞数: " + data.getInt("digg_count"));
            System.out.println("收藏数: " + data.getInt("collect_count"));
            System.out.println("评论数: " + data.getInt("comment_count"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static JSONObject getVideoData(String videoId) throws IOException {
        String url = "https://www.douyin.com/api/video/detail/?video_id=" + videoId;
        
        Request request = new Request.Builder()
                .url(url)
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                .addHeader("Referer", "https://www.douyin.com/")
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            
            String jsonData = response.body().string();
            JSONObject jsonObject = new JSONObject(jsonData);
            return jsonObject.getJSONObject("data").getJSONObject("item_info");
        }
    }
}